#include"http.hpp"

#define WWWROOT "./wwwroot/"

std::string RequestStr(const HttpRequest& req){
    std::stringstream ss;
    ss<<req._method<<" "<<req._path<<" "<<req._version<<"\r\n";
    for(auto& it:req._params){
        ss<<it.first<<": "<<it.second<<"\r\n";
    }
    for(auto& it:req._headers){
        ss<<it.first<<": "<<it.second<<"\r\n";
    }
    ss<<"\r\n";
    ss<<req._body;
    return ss.str();
}
void Hello(const HttpRequest& req,HttpResponse* resp){
    std::cout<<"Hello"<<std::endl;
    resp->SetContent(RequestStr(req),"text/plain");
}
void Login(const HttpRequest &req, HttpResponse *resp){
    std::cout<<"Login"<<std::endl;
    resp->SetContent(RequestStr(req), "text/plain");
}
void PutFile(const HttpRequest &req, HttpResponse *rsp) {
    std::cout<<"PutFile"<<std::endl;
    std::string pathname = WWWROOT + req._path;
    Util::WriteFile(pathname, req._body);
}
void DelFile(const HttpRequest &req, HttpResponse *rsp) {
    std::cout<<"DelFile"<<std::endl;
    rsp->SetContent(RequestStr(req), "text/plain");
}
int main(){
    HttpServer server(8080);
    server.SetThreadCount(3);
    server.SetBaseDir(WWWROOT);
    server.Get("/hello",Hello);
    // 其他的请求都不是get请求，不能直接在浏览器上发起的，通过postman软件来测试
    server.Post("/login", Login);
    server.Put("/1234.txt", PutFile);     //表示上传一个文件
    server.Delete("/1234.txt", DelFile);    //表示删除一个文件
    server.Listen();
    return 0;
}